/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Inventar.java
 *
 * Created on 07.05.2009, 16:56:31
 */

package Tabs;

import Binding.Binder;
import BusinessLayer.BusinessLayer;
import Objekte.Produkt;
import Objekte.ProduktListModel;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
import logging.Logger;
import wisi.WiSiView;

/**
 *
 * @author Benji
 */
public class Inventar extends javax.swing.JPanel {

    private ProduktListModel produktModel;
    private Binder binder = new Binder();
   


    /**Creates new form Inventar */
    public Inventar() {
        initComponents();
        Logger.debug("Lade "+ this.getName());

        produktModel = new ProduktListModel();
        this.jTable1.setModel(produktModel);
    }

    /**
     * sInventar liste füllen
     */
    public void BindTo() {
        try{
            if(!BusinessLayer.isLoggedIn())return;
            Logger.debug("BindTo");
            produktModel.setData(BusinessLayer.GetProductList(BusinessLayer.getUserId()));
        }
        catch(Exception e){
            Logger.error("BindTo - Products", e);
        }

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        IDLabel = new javax.swing.JLabel();
        NameLabel = new javax.swing.JLabel();
        PreisLabel = new javax.swing.JLabel();
        BeschreibungLabel = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        mengeLabel = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jSpinner1 = new javax.swing.JSpinner();
        produceButton = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        rankLabel = new javax.swing.JLabel();
        startAuctionButton = new javax.swing.JButton();
        auctionTTLSpinner = new javax.swing.JSpinner();
        jLabel9 = new javax.swing.JLabel();
        jLabelForIcon = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wisi.WiSiApp.class).getContext().getResourceMap(Inventar.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        IDLabel.setText(resourceMap.getString("IDLabel.text")); // NOI18N
        IDLabel.setName("IDLabel"); // NOI18N

        NameLabel.setText(resourceMap.getString("NameLabel.text")); // NOI18N
        NameLabel.setName("NameLabel"); // NOI18N

        PreisLabel.setText(resourceMap.getString("PreisLabel.text")); // NOI18N
        PreisLabel.setName("PreisLabel"); // NOI18N

        BeschreibungLabel.setText(resourceMap.getString("BeschreibungLabel.text")); // NOI18N
        BeschreibungLabel.setName("BeschreibungLabel"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        mengeLabel.setText(resourceMap.getString("mengeLabel.text")); // NOI18N
        mengeLabel.setName("mengeLabel"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jSpinner1.setName("jSpinner1"); // NOI18N

        produceButton.setText(resourceMap.getString("produceButton.text")); // NOI18N
        produceButton.setName("produceButton"); // NOI18N
        produceButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                produceButtonActionPerformed(evt);
            }
        });

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        rankLabel.setText(resourceMap.getString("rankLabel.text")); // NOI18N
        rankLabel.setName("rankLabel"); // NOI18N

        startAuctionButton.setText(resourceMap.getString("startAuctionButton.text")); // NOI18N
        startAuctionButton.setName("startAuctionButton"); // NOI18N
        startAuctionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startAuctionButtonActionPerformed(evt);
            }
        });

        auctionTTLSpinner.setName("auctionTTLSpinner"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabelForIcon.setText(resourceMap.getString("jLabelForIcon.text")); // NOI18N
        jLabelForIcon.setFocusable(false);
        jLabelForIcon.setName("jLabelForIcon"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(NameLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PreisLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mengeLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(IDLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(6, 6, 6)
                        .addComponent(BeschreibungLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(produceButton, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(startAuctionButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 210, Short.MAX_VALUE)
                        .addComponent(jLabelForIcon))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rankLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(auctionTTLSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(IDLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(NameLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(PreisLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(BeschreibungLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(mengeLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(rankLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(auctionTTLSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(produceButton)
                            .addComponent(startAuctionButton)))
                    .addComponent(jLabelForIcon, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents


    /**
     * ein Produkt in der DetailView anzeigen
     * @param evt
     */
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        Produkt p = produktModel.getRow(this.jTable1.getSelectedRow());

        Logger.trace("Lade Produkt in DetailView");
        binder.BindTo(IDLabel, Integer.toString(p.getID()));
        binder.BindTo(NameLabel, p.getName());
        binder.BindTo(PreisLabel, Integer.toString(p.getPrice()));
        binder.BindTo(BeschreibungLabel, p.getDescription());
        binder.BindTo(mengeLabel, Integer.toString(p.getAmount()));
        binder.BindTo(rankLabel, Integer.toString(p.getRequiredRank()));
        if(p.getIcon() != null){
            jLabelForIcon.setText("");
            binder.BindTo(jLabelForIcon, p.getIcon());
        }
        else{
            jLabelForIcon.setText("Kein Bild");
            jLabelForIcon.setIcon(null);
        }

        //this.IDLabel.setText(Integer.toString(p.getID()));
        //this.NameLabel.setText(p.getName());
        //this.PreisLabel.setText(Integer.toString(p.getPrice()));
        //this.BeschreibungLabel.setText(p.getDescription());
        //this.mengeLabel.setText(Integer.toString(p.getAmount()));
        //this.rankLabel.setText(Integer.toString(p.getRequiredRank()));

        
    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * ausgewähltes Produkt produzieren
     * 
     * @param evt
     */
    private void produceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_produceButtonActionPerformed
        Produkt p = null;
        int amount = 0;

        try{
            p = produktModel.getRow(this.jTable1.getSelectedRow());
            amount = Integer.parseInt(this.jSpinner1.getValue().toString());
            if(amount <= 0){
                Logger.user("Produzieren Sie mehr als 0 Produkte");
            } else{
                // ausgewähltes Prodult produzieren
                int i = BusinessLayer.produce(p, amount);
                switch(i){
                    case 1:
                        Logger.user(p.getName() + " produziert");
                        Logger.trace("produziere Produkt");
                        break;
                    case -9000:
                        Logger.user(p.getName() + " kann nicht Produziert werden");
                        break;
                    case -9001:
                        Logger.user("Zu wenig Geld fuer " + p.getName());
                        break;
                    case -9002:
                        Logger.user("Der Rang ist zu niedrig");
                        break;
                    case -9999:
                        Logger.user("-9999 - Fehler - Others in der DB");
                        break;
                    default:
                        Logger.user("Es geschah etwas unerwartetes");
                        break;
                }

                
                //wenn Prodziert wird alle listen neu laden - es hat sich wa sverändert
                WiSiView.refreshAll();
                Logger.trace("refreshAll() from " + this.getName());
            }

        } catch(ArrayIndexOutOfBoundsException ex){
            Logger.user("Bitte ein Produkt auswählen");
            Logger.trace("User hat kein Produkt ausgewählt");
        }

        
}//GEN-LAST:event_produceButtonActionPerformed

    /**
     * mit dem atuellen PRodukt eine auktion starten
     * @param evt
     */
    private void startAuctionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startAuctionButtonActionPerformed
        Produkt p = null;
        int amount = 0;
        int dauer = 0;

        try{
            p = produktModel.getRow(this.jTable1.getSelectedRow());
            amount = Integer.parseInt(this.jSpinner1.getValue().toString());
            dauer = Integer.parseInt(this.auctionTTLSpinner.getValue().toString());
            if(amount <= 0){
                Logger.user("Wählen Sie mehr als 0 Items");
            }
            else if(dauer <= 0){
                Logger.user("Wählen Sie mehr als 0 Stunden für die Auktion");
            }
            else{
                // ausgewähltes Produkt für eine auktion frei gebenproduzieren
                int i = BusinessLayer.startAuction(p, amount, dauer);
                Logger.debug("Inventar - TRY start autkion-------------------------------");
                switch(i){
                    case 1:
                        Logger.user(p.getName() + " wurde für eine Auktion frei gegeben");
                        break;
                    case -9024:
                        Logger.user("Sie besitzen von " + p.getName() + " zu wenig Stück");
                        break;
                    case -9025:
                        Logger.user("Die Auktion liegt in der Vergangenheit - geht nicht");
                        break;
                    case -9999:
                        Logger.user("-9999 - Fehler - Others in der DB");
                        break;
                    default:
                        Logger.user("Es geschah etwas unerwartetes");
                        break;
                }
                WiSiView.refreshAll();
                Logger.trace("RefreshAll from " + this.getName());
            }

        } catch(ArrayIndexOutOfBoundsException ex){
            Logger.user("Bitte ein Produkt auswählen");
        }
}//GEN-LAST:event_startAuctionButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel BeschreibungLabel;
    private javax.swing.JLabel IDLabel;
    private javax.swing.JLabel NameLabel;
    private javax.swing.JLabel PreisLabel;
    private javax.swing.JSpinner auctionTTLSpinner;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelForIcon;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel mengeLabel;
    private javax.swing.JButton produceButton;
    private javax.swing.JLabel rankLabel;
    private javax.swing.JButton startAuctionButton;
    // End of variables declaration//GEN-END:variables

}
